﻿Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

Public Class FormPeralatan
    Inherits PageTemplate
    Dim isTambah As Boolean = False
    Dim vID As String = ""
    Dim vALAT As String = ""
    Dim vJUMLAH As String = ""
    Dim vKETERANGAN As String = ""

    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click
        If Not cani Then
            MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
            'ClearLayout("2")
            Exit Sub
        End If
        isTambah = True
        'txtID.Properties.ReadOnly = False 
        clear_input_box()
        'txtID.Hide()
        'txtID.Visible = False        
        'getnotrx()
        LayoutControlItem3.HideToCustomization()

    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        Dim cek As String = "insert"
        Dim hasilcekinput As Boolean
        hasilcekinput = cekdata(cek)

        If hasilcekinput = False Then
            MsgBox("Entri Data Tidak Lengkap", MsgBoxStyle.Exclamation, "Informasi")
            Return
        End If

        Dim eksekusi As String = ""
        Dim eksekusi2 As String = ""
        Dim keterangan As String = ""
        Dim bf As New BinaryFormatter
        Dim ms As New MemoryStream
        Dim bytearr As Byte()
        If meKETERANGAN.Lines.Count > 0 Then
            ms = New MemoryStream
            bf.Serialize(ms, meKETERANGAN.Lines)
            bytearr = ms.ToArray
            Dim vmemo As String = String.Empty
            For i As Integer = 0 To meKETERANGAN.Lines.Count - 1
                vmemo += meKETERANGAN.Lines(i).ToString
                If i < meKETERANGAN.Lines.Count - 1 Then
                    vmemo += "\n"
                End If
            Next
            keterangan = vmemo
        Else
        End If
        If isTambah = True Then
            eksekusi = "insert"
            eksekusi2 = "Menambah"
        Else
            eksekusi = "update"
            eksekusi2 = "Mengubah"
        End If

        If eksekusi = "insert" Then
            If Not cani Then
                MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
                load_data()
                
                Exit Sub
            End If
        ElseIf eksekusi = "update" Then
            If Not canu Then
                MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
                load_data()

                Exit Sub
            End If
        End If

        If MsgBox("Ingin " & eksekusi2 & " Data Peralatan UKS ?", MsgBoxStyle.OkCancel, "Informasi") = MsgBoxResult.Ok Then
            SimpanData(eksekusi)
            load_data()
        End If
        
    End Sub

    Private Sub inisial()
        vID = txtID.Text
        vJUMLAH = txtJUMLAH.Text
        vALAT = txtNAMA_ALAT.Text
        vKETERANGAN = meKETERANGAN.Text
    End Sub

    Private Sub SimpanData(ByVal jnsSimpan As String)
        Dim strQuery As Object()
        Dim err As String = ""
        inisial()
        Dim dtResulExecute As New DataTable
        ''Dim MsgResult As String = ""
        Dim ValResult As String = ""

        strQuery = New Object() {jnsSimpan, replaceInput(vID), replaceInput(vALAT), replaceInput(vJUMLAH), replaceInput(vKETERANGAN)}
        If Utils.executeSP("proc_peralatan", strQuery, dtResulExecute) Then
            If Utils.RowExist(dtResulExecute) Then
                Dim MsgResult As String = "Execute Successfull"

                If MsgResult = dtResulExecute(0)(0) Then
                    MsgBox(MsgResult, MsgBoxStyle.Information, "Success")
                    If isTambah = True Or jnsSimpan = "delete" Then
                        clear_input_box()
                    End If
                Else
                    MsgBox(dtResulExecute(0)(0), MsgBoxStyle.Critical, "Error")
                End If
            End If
        End If

        

    End Sub
    Private Sub getnotrx()
        txtID.Text = ""
        txtID.Visible = False
        Dim dt As New DataTable
        If Utils.GetFunction("getidalat", New Object() {}, dt) Then
            If Utils.RowExist(dt) Then
                txtID.Text = dt(0)(0)
            End If
        End If
    End Sub
    Private Sub clear_input_box()

        'txtID.Properties.ReadOnly = False
        txtID.Text = ""
        txtJUMLAH.Text = "0"
        txtNAMA_ALAT.Text = ""
        meKETERANGAN.Text = ""
        isTambah = True
        SimpleButton3.Enabled = False
    End Sub

    Private Sub load_data()
        Dim dtkegiatan As New DataTable
        Dim strQuery As String()
        Dim err As String = ""
        strQuery = New String() {"*", "dataperalatan", "no", ""}
        If Utils.executeSP("proc_searchall", strQuery, dtkegiatan) Then
            'If Utils.RowExist(dtkegiatan) Then
            GridControl1.DataSource = dtkegiatan
            GridControl1.Refresh()
            GridControl1.RefreshDataSource()
            GridView1.RefreshData()
            'End If
        End If
    End Sub

    Private Function cekdata(ByVal jnscek As String) As Boolean
        Dim hasilcek As Boolean = True
        If jnscek = "insert" Then            
            If txtJUMLAH.Text = "" Then Return False
            If txtNAMA_ALAT.Text = "" Then Return False
        ElseIf jnscek = "delete" Then
            If txtID.Text = "" Then Return False
        End If
        Return hasilcek
    End Function
    Dim cani As Boolean = False
    Dim canu As Boolean = False
    Dim cand As Boolean = False
    Private Sub FormPeralatan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        load_data()
        Dim dr() As DataRow
        If Utils.RowExist(LocalSession.dtrole) Then
            dr = LocalSession.dtrole.Select("menuid = '" & LocalSession.PublicMenuId & "'")
            If dr.Length > 0 Then
                '       dt = dr.CopyToDataTable
                cani = IIf(IsDBNull(dr(0)("to_insert")), 0, dr(0)("to_insert"))
                canu = IIf(IsDBNull(dr(0)("to_update")), 0, dr(0)("to_update"))
                cand = IIf(IsDBNull(dr(0)("to_delete")), 0, dr(0)("to_delete"))
            End If
        End If
    End Sub

    Private Sub GridView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.Click
        Dim obj As DataRow
        Dim i As Integer
        If LayoutControlItem3.IsHidden Then
            LayoutControlItem3.RestoreFromCustomization(LayoutControlGroup2)
            LayoutControlItem3.Move(LayoutControlItem4, DevExpress.XtraLayout.Utils.InsertType.Top)

        End If
       
        txtID.Properties.ReadOnly = True
        isTambah = False
        i = GridView1.FocusedRowHandle
        obj = GridView1.GetDataRow(i)
        Dim j As Integer
        j = GridView1.RowCount
        If j > 0 Then
            txtID.Text = obj(0)
            txtNAMA_ALAT.Text = obj(1)
            txtJUMLAH.Text = obj(2)
            meKETERANGAN.Text = obj(3)
            SimpleButton3.Enabled = True
        End If
        'txtID.Visible = True

    End Sub

    Private Sub SimpleButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton3.Click
        If Not cand Then
            MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
            load_data()
            Exit Sub
        End If

        Dim hasilcekinput As Boolean = False
        hasilcekinput = cekdata("delete")
        If hasilcekinput = False Then
            MsgBox("Entri Data Tidak Lengkap", MsgBoxStyle.Exclamation, "Informasi")
            Return
        End If
        If MsgBox("Ingin Menghapus Data Peralatan Ruang UKS?", MsgBoxStyle.OkCancel, "Informasi") = MsgBoxResult.Ok Then
            SimpanData("delete")
            load_data()
        End If
        
    End Sub

    Private Function replaceInput(ByVal teks As String) As String
        teks = teks.Replace("\", "\\")
        teks = teks.Replace("'", "\'")
        Return teks
    End Function
End Class
